package com.example.demo.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.example.demo.exception.ServiceException;
import com.example.demo.mapper.LoginMapper;
import com.example.demo.pojo.Admin;
import com.example.demo.pojo.login.LoginDTO;
import com.example.demo.pojo.login.LoginRequest;
import com.example.demo.service.LoginService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;




@Service
public class LoginServicelmpl implements LoginService {
    private static final String PASS_SALT="gege";
    @Autowired
    LoginMapper loginMapper;
    @Override
    public LoginDTO login(LoginRequest request) {

        //m5加密
        request.setPassword(SecureUtil.md5(request.getPassword()+PASS_SALT));
        Admin admin = loginMapper.login(request);
        if(admin == null){
            throw new ServiceException("用户名或密码错误");
        }
        LoginDTO loginDTO = new LoginDTO();
        BeanUtils.copyProperties(admin, loginDTO);
        return loginDTO;
    }
}
